package com.wh.wisdomsite.xbox.server.controller.me;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.me.entity.CraneProject;
import com.wh.wisdomsite.xbox.me.service.ICraneProjectService;
import com.wh.wisdomsite.xbox.pojo.me.CraneProjectIn;
import com.wh.wisdomsite.xbox.pojo.me.CraneRecordDataIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import com.wh.wisdomsite.xbox.server.util.DateUtils;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import com.wh.wisdomsite.xbox.server.util.WrapperUtil;
import com.wh.wisdomsite.xbox.us.entity.Company;
import com.wh.wisdomsite.xbox.us.service.ICompanyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 起重机械实时数据
 * @package com.wh.wisdomsite.xbox.server.controller.me
 * @author 蔡道行
 * @version V1.0
 * @date 17:06 2018/9/5
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 */

@Controller
@RequestMapping(value = "/craneRecordData")
public class CraneRecordDataController {
    private static final Log logger = LogFactory.getLog(CraneRecordDataController.class);

    @Autowired
    private IProjectService iProjectService;

    @Autowired
    private ICraneProjectService iCraneProjectService;

    @Autowired
    private ICompanyService iCompanyService;

    /**
     * 跳转到起重机械设备实时数据
     * @return
     */
    @RequiresPermissions("rules:me_craneRecordData_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index() {
        return "me/craneRecordData/craneRecordDataList";
    }

    /**
     * 获取起重机械设备实时数据列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:me_craneRecordData_find")
    @RequestMapping(value = "/craneRecordDataList.do")
    public void craneRecordDataList(HttpServletResponse response, CraneRecordDataIn in) {
        try {
            logger.info("加载起重机械设备实时数据列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<Project> page = new Page<Project>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            WrapperUtil<Project> ewProject = new WrapperUtil<Project>();
            ewProject.setEntity(new Project());
            ewProject.setTableAlias("p");
            //获取公司编码
            String compCode = null;
            Integer sessionCompId = UserUtils.getCurrentUser().getCompanyId();
            if(null != sessionCompId && sessionCompId != 0){
                Company company = iCompanyService.selectById(sessionCompId);
                compCode = company.getCompCode();
            }
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewProject.eq("prId",in.getPrId());
            }
            ewProject.eq("dataState",1);
            Page<Map<String, Object>> projectPage = iProjectService.selectPageByCode(page, ewProject,compCode,null);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (projectPage.getRecords().size() > 0) {
                for(Map<String, Object> project : projectPage.getRecords()){
                    JSONObject object = new JSONObject(project);
                    if(project.get("unitBuild") != null){
                        object.put("unitBuild",new JSONObject(project.get("unitBuild")));
                    }
                    if(project.get("unitOperator") != null){
                        object.put("unitOperator",new JSONObject(project.get("unitOperator")));
                    }
                    if(project.get("unitSupervisor") != null){
                        object.put("unitSupervisor",new JSONObject(project.get("unitSupervisor")));
                    }
                    array.put(object);
                }
            }
            json.put("total", projectPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载起重机械设备实时数据列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 起重机械设备实时数据详情
     * @param prId
     * @param request
     * @return
     */
    @RequestMapping("craneRecordDataDesc.do")
    public String craneRecordDataDesc(String prId, HttpServletRequest request) {
        try{
            logger.info("起重机械设备实时数据详情-->>开始");
            Map<String , Object> craneRecordData = iProjectService.getMapById(prId);
            request.setAttribute("craneRecordData", craneRecordData);
            logger.info("起重机械设备实时数据详情-->>结束");

        }catch (Exception e){
            e.printStackTrace();
        }
        return "me/craneRecordData/craneRecordDataDesc";
    }

    /**
     * 根据项目id获取起重机械设备
     * @param in
     */
    @RequestMapping(value = "/craneRecordList.do")
    public void craneRecordList(HttpServletResponse response, CraneProjectIn in) {
        try {
            logger.info("获取起重机械设备列表-->>开始");
            //设置条件
            EntityWrapper<CraneProject> ewCraneProject = new EntityWrapper<CraneProject>();
            ewCraneProject.setEntity(new CraneProject());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewCraneProject.eq("cp.prId",in.getPrId());
            }
            if(null != in.getOnLineStatus()){
                ewCraneProject.in("cp.onLineStatus",String.valueOf(in.getOnLineStatus()));
            }
            ewCraneProject.eq("cp.dataState",1);
            List<CraneProject> craneProjectList = iCraneProjectService.selectList(ewCraneProject);
            JSONArray array = new JSONArray();
            for(CraneProject craneProject : craneProjectList){
                JSONObject obj = new JSONObject(craneProject);
                //在线时间
                String onlineTime = "";
                String onlineState = craneProject.getOnLineStatus();
                if("1".equals(onlineState)){
                    Date date = new Date();
                    Date dateOnline = new Date();
                    if(null != craneProject.getUpdateDate()){
                        dateOnline = craneProject.getUpdateDate();
                    }else{
                        dateOnline = craneProject.getCreateDate();
                    }
                    onlineTime = DateUtils.getDifferenceTime(dateOnline,date);
                }
                obj.put("onlineTime",onlineTime);
                array.put(obj);
            }
            ResManager.getJsonOut(response).print(array);
            logger.info("获取起重机械设备列表-->>结束");
        } catch (Exception e) {
             e.printStackTrace();
        }
    }

    /**
     * 获取设备对应的项目数据列表
     * @param request
     * @param in
     */
    @RequestMapping(value = "/toCraneDeviceList.do")
    public String toCraneDeviceList(HttpServletRequest request, CraneProjectIn in) {
        String projectId = in.getPrId();
        request.setAttribute("projectId",projectId);
        return "me/craneRecordData/craneRecordList";
    }



}

